import Toast from '@vant/weapp/toast/toast';
import Ajax from "../../../../utils/ajax"
import Util from "../../../../utils/util"
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    list:[ 1,2,3,4,5,6],
    show:false,
    sms:'',
    cur:'',
    ztFeature:{
      balconyOrientation:"",
      decorateSituation:"",
      decorateYear:"", 
      propertyConfiguration:"",
      rentalType:"",
      scenery:"",
      specialLabel:"",
    },
  },
  attached(){
    console.log("组件进入时渲染", app.globalData.ztFeature  );
     var tmp =  app.globalData.ztFeature
      tmp.balconyOrientations =[];
      tmp.decorateSituations = [];
      tmp.propertyConfigurations  = [];
      tmp.rentalTypes  = [];
      tmp.scenerys = [];
      tmp.specialLabels  = [] 
     if(tmp.balconyOrientation){ tmp.balconyOrientations = tmp.balconyOrientation.split(",").map(e=>{ return { choose:e.startsWith("*"), text:e.replace("*",'') } }) }
     if(tmp.decorateSituation){ tmp.decorateSituations = tmp.decorateSituation.split(",").map(e=>{ return { choose:e.startsWith("*"), text:e.replace("*",'') } }) }
     if(tmp.propertyConfiguration){ tmp.propertyConfigurations = tmp.propertyConfiguration.split(",").map(e=>{ return { choose:e.startsWith("*"), text:e.replace("*",'') } }) }
     if(tmp.rentalType){ tmp.rentalTypes = tmp.rentalType.split(",").map(e=>{ return { choose:e.startsWith("*"), text:e.replace("*",'') } }) }
     if(tmp.scenery){ tmp.scenerys = tmp.scenery.split(",").map(e=>{ return { choose:e.startsWith("*"), text:e.replace("*",'') } }) }
     if(tmp.specialLabel){ tmp.specialLabels = tmp.specialLabel.split(",").map(e=>{ return { choose:e.startsWith("*"), text:e.replace("*",'') } }) }

     this.setData({ ztFeature: tmp,   });
  },
  /**
   * 组件的方法列表
   */
  methods: {
    saveData(tmp,cur){
      var key = cur.substr(0,cur.length-1)
      tmp[key] = tmp[cur].map(e=>{ return  e.choose? '*' + e.text:e.text }).join(",")
      this.setData({ show: false,sms:'',ztFeature:tmp })
      app.globalData.ztFeature = tmp; 
    },
    add(e){
      this.setData({ cur:e.currentTarget.dataset.id,show: true })
    },
    chooseItem(e){
      var cur = e.currentTarget.dataset.id
      var index = e.currentTarget.dataset.index
      var tmp = this.data.ztFeature
      tmp[cur][index].choose = !tmp[cur][index].choose 
      this.saveData(tmp,cur)
    },
    del(e){
      var cur = e.currentTarget.dataset.id
      var index = e.currentTarget.dataset.index
      var tmp = this.data.ztFeature
      tmp[cur].splice(index,1)
      this.saveData(tmp,cur)
    },
    newSms(e){
      this.setData({ sms:e.detail })
    },
    confirm(){
      var cur = this.data.cur  
      var tmp = this.data.ztFeature
      tmp[cur].push({ text:this.data.sms,choose:true}) 
      this.saveData(tmp,cur)
    }
  }
})
